﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Emu8080.Compiler.Structures
{
    public abstract class CompilerCommand : ProcessingUnit
    {
        public abstract CompilerCommands Command { get; }
    }

    public class CompilerCommandOrg : CompilerCommand
    {
        public UInt16 Address { get; set; }

        public override CompilerCommands Command
        {
            get
            {
                return CompilerCommands.ORG;
            }
        }
    }

    public class CompilerCommandDb : CompilerCommand, IProcessingUnitFundamental
    {
        public UInt16 Address { get; set; }
        public CommandDbOperand Operand { get; set; }

        public override CompilerCommands Command
        {
            get
            {
                return CompilerCommands.DB;
            }
        }

        public byte[] GetMachineCodes()
        {
            if (Operand.Type == CommandDbOperand.CommandDbOperandType.NumberArray)
            {
                var ba = (byte[])Operand.Value;
                return ba;
            }
            else
            {
                return new byte[0]; // TODO convert string to ba
            }
        }
    }

    public class CompilerCommandDw : CompilerCommand, IProcessingUnitFundamental
    {
        public UInt16 Address { get; set; }
        public ushort [] Operand { get; set; }

        public override CompilerCommands Command
        {
            get
            {
                return CompilerCommands.DW;
            }
        }


        public byte[] GetMachineCodes()
        {
            var ba = new byte[Operand.Length * 2];
            for (int i = 0; i < Operand.Length; i++)
            {
                ba[i*2] = (byte)(Operand[i] & 0xFF);
                ba[i*2 + 1] = (byte)(Operand[i] >> 8);
            }
            return ba;
        }
    }

    public class CompilerCommandEnd : CompilerCommand
    {
        public ImmediateOperand16bit Address { get; set; }

        public override CompilerCommands Command
        {
            get
            {
                return CompilerCommands.END;
            }
        }
    }
}
